$(function(){
	
	$('#username').blur(function(){
		var username = $.trim($(this).val());
		if(username.length > 0){
			$.ajax({
				url:"checkUsername.action",
				data:{adminId:username},
				type:'post',
				cache:false,
				success: function(data){
					if("exist" == data){
						alert("该用户名已经存在.");
						$('#username').val("");
						$('#username').focus();
					}
				}
			})
		}
	})
	
	$('#saveAdmin').click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		var rePassword = $("#rePassword").val();
		
		if ($.trim(username).length < 1) {
			alert("请填写用户名");
			$('#username').focus();
			return false;
		}else if ($.trim(password).length < 1) {
			alert("请填写密码");
			$('#password').focus();
			return false;
		}else if ($.trim(rePassword).length < 1) {
			alert("请再次填写密码");
			$('#rePassword').focus();
			return false;
		}else if (password != rePassword) {
			alert("两次输入的密码不一致,请检查");
			$('#password').focus();
			return false;
		}else{
			$.ajax({
				url:"addAdmin.action",
				data:$("#adminform").serialize(),
				type:'post',
				cache:false,
				success: function(data){
					if("success" == data){
						alert("保存成功");
						location.href = 'listAdmin.jsp';
					}else{
						alert("网络繁忙,请稍候重试");
					}
				}
			})
		}
	})
	
	$('#updateAdmin').click(function(){
		var password = $("#password").val();
		var rePassword = $("#rePassword").val();
		
		if (password != rePassword) {
			alert("两次输入的密码不一致,请检查");
			$('#password').focus();
			return false;
		}else{
			$.ajax({
				url:"updateAdmin.action",
				data:$("#adminform").serialize(),
				type:'post',
				cache:false,
				success: function(data){
					if("success" == data){
						alert("保存成功");
						location.href = 'listAdmin.jsp';
					}else if("self" == data){
						alert("保存成功");
					}else{
						alert("网络繁忙,请稍候重试");
					}
				}
			})
		}
	})
	
	$("#backAdmin").click(function(){
		history.back();
	})
	
})
